/*
 * Fast car reverse image preview module
 *
 * Copyright (C) 2015-2018 AllwinnerTech, Inc.
 *
 * Contacts:
 * Zeng.Yajian <zengyajian@allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

struct buffer_manager *alloc_buffer_manager(void)
{
	struct buffer_manager *bm;

	bm = kzalloc(sizeof(struct buffer_manager), GFP_KERNEL);
	if (!bm) {
		printk("%s: alloc failed\n", __func__);
		goto _out;
	}

	spin_lock_init(&bm->lock);

	bm->dequeue_buffer = dequeue_buffer;
	bm->queue_buffer   = queue_buffer;
	bm->acquire_buffer = acquire_buffer;
	bm->release_buffer = release_buffer;

	/* TODO, alloc memory for buffer node */

_out:
	return bm;
}

static struct buffer_node *dequeue_buffer(struct buffer_manager *bm)
{
	struct buffer_node *retval = NULL;

	spin_lock(&bm->lock);
	if (bm->buffer[_FREE_BUFF]) {
		retval = bm->buffer[_FREE_BUFF];
		bm->buffer[_FREE_BUFF] = NULL;
	}
	spin_unlock(&bm->lock);

	if (!retval)
		printk("dequeue: buffer maybe over write\n");

	return retval;
}

static void queue_buffer(struct buffer_manager *bm, struct buffer_node *buff)
{
	
}
